<?php

namespace App\Http\Controllers;

use App\Model\AdminUser;
use App\Model\Article;
use App\Model\ArticlePraise;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class BlogController extends Controller
{
    /**
     * 首页。
     *
     * @return Response
     */
    public function index()
    {
        $article_info = Article::get_newest_article_info();
        if(isset($article_info->article_id) && $article_info->article_id > 0){
            return redirect()->action('BlogController@blog', $article_info->article_id);
        }else{
            return view('blog.error', ['msg'=>'暂无文章。']);
        }
        return view('blog.index');
    }	
	
    /**
     * 详情。
     *
     * @param  int  $id
     * @return Response
     */
    public function blog($id)
    {
		$blog_info = Article::get_article_info($id);
        $blog_list = Article::get_article_list();
        return view('blog.blog', ['blog_info'=>$blog_info, 'blog_list'=>$blog_list]);
    }	

    /**
     * 登录。
     *
     * @return Response
     */
    public function login()
    {
        return view('blog.login');
    }

    /**
     * 登录验证。
     *
     * @return Response
     */
    public function signin(Request $request)
    {
        $user_name = $request->input('user_name');
        $password = md5($request->input('password'));
        $adminuser = new AdminUser;
        $user_info = $adminuser
                ->where('user_name', $user_name)
                ->where('password', $password)
                ->first();
        if($user_info){
            $request->session()->put('user_id', $user_info->user_id);
            $request->session()->put('user_name', $user_info->user_name);
            return redirect()->action('BlogController@home');
        }else{
            return view('blog.error', ['msg'=>'登录失败，帐号密码不正确。']);
        }
    }

    /**
     * 退出登录。
     *
     * @return Response
     */
    public function signout(Request $request)
    {
        $request->session()->forget('user_id');
        if($request->session()->has('user_id')){
            return view('blog.error', ['msg'=>'退出失败。']);
        }else{
            return view('blog.error', ['msg'=>'退出成功。']);
        }
    }

    /**
     * 个人中心。
     *
     * @return Response
     */
    public function home()
    {
        $article_list = Article::get_article_list();
        return view('blog.home', ['article_list'=>$article_list]);
    }

    /**
     * 编辑。
     *
     * @return Response
     */
    public function edit($id)
    {
        $article_info = Article::get_article_info($id);
        return view('blog.edit', ['article_info'=>$article_info]);
    }

    /**
     * 更新。
     *
     * @return Response
     */
    public function update(Request $request)
    {
        $article_id = $request->input('article_id');
        $title = $request->input('title');
        $author = $request->input('author');
        $content = $request->input('content');
        $article = new Article;
        $article->where('article_id', $article_id)->update(['title'=>$title, 'author'=>$author, 'content'=>$content]);
        return redirect()->action('BlogController@home');
    }

    /**
     * 添加。
     *
     * @return Response
     */
    public function add()
    {
        return view('blog.add');
    }

    /**
     * 插入。
     *
     * @return Response
     */
    public function insert(Request $request)
    {
        $title = $request->input('title');
        $author = $request->input('author');
        $content = $request->input('content');
        $add_time = time();
        $article = new Article;
        $article->insert(['title'=>$title, 'author'=>$author, 'content'=>$content, 'add_time'=>$add_time]);
        return redirect()->action('BlogController@home');
    }

    /**
     * 删除。
     *
     * @param  int  $id
     * @return Response
     */
    public function delete($id)
    {
        $article = new Article;
        $article->where('article_id', $id)->delete();
        return redirect()->action('BlogController@home');
    }

    /**
     * 点赞。
     *
     * @param  int  $id
     * @return Response
     */
    public function praise(Request $request, $id)
    {
        /* 获取定位信息 */
        $ip = $request->getClientIp();
        $ipContent = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
        $ipArray = json_decode($ipContent, true);
        if(isset($ipArray['data']['city'])){
            $city = $ipArray['data']['city'];
        }else{
            $city = '';
        }

        $session_id = session()->getId();
        $article_praise = new ArticlePraise;
        $praise_status = $article_praise->check_praise_status($id, $session_id);
        if(empty($praise_status)){
            //插入记录
            $article_praise->insert(['id'=>NULL, 'article_id'=>$id, 'ip'=>$ip, 'city'=>$city, 'session_id'=>$session_id, 'add_time'=>time()]);
            //更新总数
            $article = new Article;
            $article_info = $article->get_article_info($id);
            $article->where('article_id', $id)->update(['praise_num'=>$article_info->praise_num + 1]);
            $success = 1;
        }else{
            $success = 0;
        }
        return response()->json(['success' => $success]);
    }
}